package com;

import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet (value = "/Insert")
public class Insert extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        int courseid = Integer.parseInt(request.getParameter("courseid"));
        String name = request.getParameter("name").trim();
        int teacherid = Integer.parseInt(request.getParameter("teacherid").trim());
        System.out.println(courseid + name + teacherid);
        Connection con = null;
        PreparedStatement psm1 = null;

        try {
            con = JdbcUtil.getConnection();
            psm1 = con.prepareStatement("INSERT INTO course VALUES(?,?,?)");
            psm1.setInt(1, courseid);
            psm1.setString(2, name);
            psm1.setInt(3, teacherid);
            int i = psm1.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("/InsertCourse").forward(request,response);
    }
}